<?php
namespace app\manage\validate;
use think\Validate;
class SystemValidate extends Validate
{
     protected $rule = [
        'cnname'  =>  'require|chs|lizhiliUnique:system',
        'enname' =>  'require|alpha|lizhiliUnique:system',
        'type' =>  'require',
    ];
	 protected $message  =   [
        'cnname.require' => '中文名称必须填写',
        'cnname.chs'     => '中文名称必须为汉字',
        'cnname.lizhiliUnique'     => '中文名称已经存在，不能重复！',
        'enname.require' => '英文名称必须填写',
        'enname.alpha'     => '英文名称必须为字母',
        'enname.lizhiliUnique'     => '英文名称已经存在，不能重复！',
        'type.require'   => '类型必须填写',
    ];
	protected $scene = [
        'edit'  =>  [
			'cnname'  =>  'require|chs',
	        'enname' =>  'require|alpha',
	        'type' =>  'require',
        ],
        
    ];
    // 自定义验证规则
    protected function lizhiliUnique($value, $rule, $data=[],$field='',$desc='')
    {
        $where = [['delete_time','=',Null],[$field,'=',$value]];
        $str='\\app\\model\\'.ucfirst($rule).'Model';
        $count = (new $str())->where($where)->count();
        if($count){
            return false;
        }
        return true;
    }
}
